package com.wtwd.campus.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wtwd.campus.common.Constants;
import com.wtwd.campus.common.RespCode;
import com.wtwd.campus.common.RespCodeEntity;
import com.wtwd.campus.dao.AttendanceProductMapper;
import com.wtwd.campus.entity.Attendance;
import com.wtwd.campus.entity.AttendanceProduct;
import com.wtwd.campus.entity.AttendanceProductVO;
import com.wtwd.campus.model.vo.PageDataVO;
import com.wtwd.campus.model.vo.SysUserVO;
import com.wtwd.campus.service.AttendanceProductService;
import com.wtwd.campus.service.RedisService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

@Service
public class AttendanceProductServiceImpl implements AttendanceProductService {

    @Autowired
    private AttendanceProductMapper attendanceMapper;
    @Autowired
    private RedisService redisService;


    @Override
    public PageDataVO<AttendanceProductVO> getAttendanceProductList(Integer schoolId, String productNo, Integer currPage, Integer pageSize) {
        Page<AttendanceProductVO> page = new Page<>(currPage, pageSize);
        IPage<AttendanceProductVO> attendanceProductVOIPage = attendanceMapper.getAttendanceProductList(page, schoolId, productNo);
        // 处理在线离线问题
        parseAttendanceProductOnlineStatus(attendanceProductVOIPage);
        PageDataVO<AttendanceProductVO> pageDataVO = new PageDataVO<>();
        pageDataVO.setTotal(page.getTotal());
        pageDataVO.setRows(attendanceProductVOIPage.getRecords());
        return pageDataVO;
    }

    @Override
    public RespCodeEntity addAttendanceProduct(AttendanceProduct attendanceProduct, Integer userId) {
        // 判断productNo是否存在
        boolean productIsExist = deviceExists(attendanceProduct.getProductNo());
        if (!productIsExist) {
            return new RespCodeEntity(RespCode.ATTENDANCE_PRODUCT_EXISTS);
        }
        Long currentTimeStamp = System.currentTimeMillis();
        attendanceProduct.setCreateTime(currentTimeStamp);
        attendanceProduct.setUpdateTime(currentTimeStamp);
        attendanceProduct.setCreateUser(userId);
        attendanceProduct.setUpdateUser(userId);
        int i = attendanceMapper.addAttendanceProduct(attendanceProduct);
        return new RespCodeEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    @Override
    public RespCodeEntity deleteAttendanceProduct(Integer productId) {
        int i = attendanceMapper.delAttendanceProduct(productId);
        return new RespCodeEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    @Override
    public RespCodeEntity updateAttendance(AttendanceProduct attendanceProduct) {
        // 判断productNo是否存在
        AttendanceProduct attendance = attendanceMapper.getAttendProductByProductNo(attendanceProduct.getProductNo());
        if (attendance != null && attendance.getProductId() != attendanceProduct.getProductId()) {
            return new RespCodeEntity(RespCode.ATTENDANCE_PRODUCT_EXISTS);
        }
        int i = attendanceMapper.updateAttendanceProduct(attendanceProduct);
        return new RespCodeEntity(i > 0 ? RespCode.SUCCESS : RespCode.FAILED);
    }

    @Override
    public AttendanceProduct getAttendanceProductByProNo(String proNO) {
        return attendanceMapper.getAttendProductByProductNo(proNO);
    }

    /**
     * 转换考勤机
     *
     * @param attendanceProductVOIPage
     */
    private void parseAttendanceProductOnlineStatus(IPage<AttendanceProductVO> attendanceProductVOIPage) {
        List<AttendanceProductVO> records = attendanceProductVOIPage.getRecords();
        if (records != null && records.size() > 0) {
            // redis查出所有在线的考勤机
            Set onlineSet = redisService.keys(Constants.ATTENDANCE_ONLINE_PRE);
            records.stream().forEach(
                    record -> {
                        boolean contains = onlineSet.contains(Constants.ATTENDANCE_ONLINE_PRE + record.getProductNo());
                        record.setOnlineStatus(contains ? 1 : 0);
                    }
            );
        }
    }

    /**
     * 校验考勤机是否存在
     *
     * @param productNo
     * @return
     */
    private boolean deviceExists(String productNo) {
        AttendanceProduct attendProductByProductNo = attendanceMapper.getAttendProductByProductNo(productNo);
        return attendProductByProductNo == null;
    }


}
